// pages/welcome/welcome.js
import { Config } from "../../utils/config";

const app = getApp()
Page({
    data: {
        mpName:"证件照",
        time: 5,
        welcomeAd: { enable: false },
    },
    // 启动倒计时
    stratTimer() {
        let _this = this;
        this.timer = setInterval(function () {
            _this.setData({
                time: _this.data.time - 1
            })
            if (_this.data.time == 0)
                _this.jmp()
        }, 1000)

    },
    jmp: function () {
        clearInterval(this.timer);//清除定时器
        wx.switchTab({
            url: '/pages/index/index',
        })
    },//跳过广告
    onLoad(options) {
        
        // 通过全局参数查看是否已经加载了数据，如果
        // 尚未加载数据，使用回调处理
        if (app.globalData.serverData) {
            this.onLoadAL()
        } else {
            app.callBack = (res) => {
                this.onLoadAL()
            }
        }
    },
    /*onLanch中执行的回调函数,设置一些初始数据 */
    onLoadAL() {
        // wx.removeStorageSync("welcome");
        const data = app.globalData.serverData;
        this.setData({
            welcomeAd: data["ad"][Config.AD_POSITION_WELCOME],
            mpName:data['config']['mpName'],
        });
        // 从本地缓存中获取是否需要显示开屏页
        let welcome = wx.getStorageSync("welcome");
        var myDate = new Date();
        if (!this.data.welcomeAd.enable || (welcome && welcome.expiretime > myDate.getTime())) {
            this.jmp()
        } else {
            wx.setStorageSync('welcome', {
                expiretime: myDate.getTime() + (Config.USER_KEEP_HOUR * 60) * 60 * 1000,
            })
            this.stratTimer();
        }

    },

})